﻿using FlexFlow.Base.Interface;
using FlexFlow.Model;
using MongoDB.Bson;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FlexFlow.DataProvider.MongoDB
{
    /// <summary>
    /// 表示用户权限提供者的MongoDb实现
    /// </summary>
    public class MongoDbUserPermissionProvider : MongoDBPhysicalModelProvider<UserPermission>,IUserPermissionProvider
    {
        public override UserPermission Create()
        {
            return new UserPermission();
        }
        /// <summary>
        /// 取得指定权限
        /// </summary>
        /// <param name="defId">
        /// 权限定义Id
        /// </param>
        /// <param name="userId">
        /// 用户Id
        /// </param>
        /// <param name="orgId">
        /// 组织机构Id
        /// </param>
        /// <param name="tag">
        /// 权限备注
        /// </param>
        /// <param name="value">
        /// 权限值
        /// </param>
        /// <returns>
        /// 表示取得的权限，如果权限不存在，返回null
        /// </returns>
        /// <remarks>
        /// userId与orgId的比较为或关系，即匹配userId后无需匹配orgId
        /// </remarks>
        public UserPermission Get(Guid defId, Guid userId, Guid orgId, string tag, bool value)
        {
            BsonRegularExpression userIdExp = new BsonRegularExpression(userId.ToString());
            BsonRegularExpression orgExp = new BsonRegularExpression(orgId.ToString());
            FilterDefinition<UserPermission> userIdFilter = Builders<UserPermission>.Filter.Regex(m=>m.Users, userIdExp);
            FilterDefinition<UserPermission> orgFilter = Builders<UserPermission>.Filter.Regex(m=>m.Users, orgExp);
            FilterDefinition<UserPermission> tagFilter = Builders<UserPermission>.Filter.Where(m => m.Tag == tag);
            FilterDefinition<UserPermission> filter = Builders<UserPermission>.Filter.Or(userIdFilter, orgFilter);
            filter = Builders<UserPermission>.Filter.And(tagFilter, filter);
            return GetCollection().FirstOrDefault(filter);
        }
        /// <summary>
        /// 更新指定的权限
        /// </summary>
        /// <param name="identity">
        /// 权限Id
        /// </param>
        /// <param name="users">
        /// 用户列表，为null则表示不更新用户
        /// </param>
        /// <param name="orgs">
        /// 组织机构列表，为null则表示不更新组织机构
        /// </param>
        /// <returns>
        /// 一个值，表示操作是否成功
        /// </returns>
        public bool Update(Guid identity, string users = null, string orgs = null)
        {
            if (users == null && orgs == null)
            {
                return true;
            }
            UpdateDefinition<UserPermission> updef = null;
            if (users != null)
            {
                updef = BuildUpdateDef(m => m.Users, users, updef);
            }
            if (orgs != null)
            {
                updef = BuildUpdateDef(m => m.Organizations, orgs, updef);
            }
            return this.Update(identity, updef);
        }
        /// <summary>
        /// 通过权限定义ID取得用户权限实例
        /// </summary>
        /// <param name="defId">
        /// 权限定义Id
        /// </param>
        /// <param name="tag">
        /// 权限定义附加信息
        /// </param>
        /// <returns>
        /// 取得的用户权限实例
        /// </returns>
        public UserPermission GetByDefineId(Guid defId, string tag)
        {
            FilterDefinition<UserPermission> filter = (FilterDefinition<UserPermission>)(m => m.DefineId == defId && m.Tag == tag);
            return this.GetCollection().FirstOrDefault(filter);
        }
    }
}
